package com.cjc.sport.serviceimpl;

import com.cjc.sport.entity.Category;
import com.cjc.sport.entity.Course;
import com.cjc.sport.entity.Shuffling;
import com.cjc.sport.entity.UserTemp;
import com.cjc.sport.exception.CourseException;
import com.cjc.sport.exception.ServiceException;
import com.cjc.sport.mapper.CategoryMapper;
import com.cjc.sport.mapper.CourseMapper;
import com.cjc.sport.mapper.ShufflingMapper;
import com.cjc.sport.mapper.UserTempMapper;
import com.cjc.sport.service.UserService;
import com.cjc.sport.util.SportUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户界面服务层实现类
 *
 * @Author: Bruse Queen
 * @Date: 2018/2/28
 */
@Service(value = "userService")
public class UserServiceImpl implements UserService {
    /**
     * 装配课程分类mapper
     */
    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 装配轮播图mapper
     */
    @Autowired
    private ShufflingMapper shufflingMapper;
    /**
     * 装配usertemp的mapper
     */
    @Autowired
    private UserTempMapper userTempMapper;
    /**
     * 装配课程mapper
     */
    @Autowired
    private CourseMapper courseMapper;
    /**
     * 引入配置文件
     */
    @Autowired
    private Environment environment;

    @Override
    public List<Category> getAllCategories() {
        // 执行查询
        List<Category> categories = categoryMapper.selectAllCategory();
        if (categories.isEmpty()) {
            throw new ServiceException("暂无相关记录!");
        }
        return categories;
    }

    @Override
    public List<Shuffling> getTrueShuffling() {
        // 执行查询
        List<Shuffling> shufflings = shufflingMapper.getTrueShuffling();
        if (shufflings.isEmpty()) {
            throw new ServiceException("暂无相关记录!");
        }
        for (Shuffling s : shufflings) {
            s.setImgUrl(environment.getProperty("img.url") + s.getImgUrl());
        }
        // 设置imgUrl
//        shufflings.forEach(s -> s.setImgUrl(environment.getProperty("img.url") + s.getImgUrl()));
        return shufflings;
    }

    @Override
    public void addUserTemp(UserTemp userTemp) {
        // 设置uuid
        userTemp.setUtId(SportUtils.getUuid());
        // 执行新增
        int i = userTempMapper.insertSelective(userTemp);
        if (i != 1) {
            throw new ServiceException("新增失败！");
        }
        return;
    }

    @Override
    public Page<Course> selectByCondition(String courseName, String categoryName,
                                          String courseTag, int pageNum, int pageSize) {
        // 需要先设置pagehelp才能用list转page
        // 设置分页
        PageHelper.startPage(pageNum, pageSize);
        // 执行查询
        List<Course> courses = courseMapper.selectActionSql(courseName, categoryName, courseTag);
        // 设置imgUrl
//        courses.forEach(c -> c.setPicUrl(environment.getProperty("img.url") + c.getPicUrl()));
        for (Course c : courses) {
            c.setPicUrl(environment.getProperty("img.url") + c.getPicUrl());
        }

        // 获取分页后集合
        Page<Course> coursePage = (Page<Course>) courses;
        if (coursePage.isEmpty()) {
            throw new CourseException("暂无相关数据！");
        }
        return coursePage;
    }
}
